#ifndef UPLOADFILE_H
#define UPLOADFILE_H
#include <QFile>
#include <QDebug>
#include <QDir>
#include <QNetworkReply>
#include <QStandardPaths>
#include <QSystemTrayIcon>
#include <QProgressDialog>
#include <QMessageBox>
#include <QTimer>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDesktopServices>
#include <QCoreApplication>
#include "qaesencryption.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <QHttpMultiPart>
#include <QNetworkAccessManager>
#include <QFileInfo>
#include <QEventLoop>
#include "./includeHeader/JlCompress.h"
#include "wait.h"
#include <QSettings>
#include "loadingdialog.h"


/*文件头结构体，预留140个字节，可扩展*/
#define FileHeadCashBufferSize	140
#define PressureDataCount	(9)
#define EveryReadDataByte 1000
//#pragma pack(1)
typedef struct _HeadInfo
{
    _HeadInfo() { Clear(); }
    short type;	// RecordType  录制类型
    float totalTime;//总时间(秒) 录制时间
    unsigned long uploadStateID;//上传数据库的数据记录ID

    //第一次校准
    float _q1_l[4];			//左脚四元数
    float _q1_r[4];			//右脚四元数
    float _accel_1_l[3];	//左脚加速度
    float _accel_1_r[3];	//右脚加速度
    //第二次校准
    float _q2_l[4];			//左脚四元数
    float _q2_r[4];			//右脚四元数
    float _accel_2_l[3];	//左脚加速度
    float _accel_2_r[3];	//右脚加速度

    void Clear()
    {
        type = 0;
        totalTime = 0.0f;
        uploadStateID = -1;
        memset(_q1_l, 0, sizeof(float)*4);
        memset(_q1_r, 0, sizeof(float)*4);
        memset(_accel_1_l, 0, sizeof(float) * 3);
        memset(_accel_1_r, 0, sizeof(float) * 3);
        memset(_q2_l, 0, sizeof(float) * 4);
        memset(_q2_r, 0, sizeof(float) * 4);
        memset(_accel_2_l, 0, sizeof(float) * 3);
        memset(_accel_2_r, 0, sizeof(float) * 3);

    }
}HeadInfo;


typedef struct _FileHead
{
    void Clear()
    {
        stcHead.Clear();
        memset(extra, 0, FileHeadCashBufferSize - sizeof(HeadInfo));
    }
    HeadInfo stcHead;
    char extra[FileHeadCashBufferSize - sizeof(HeadInfo)];

    _FileHead& operator=(const _FileHead& fileHead)
    {
        memcpy(&(this->stcHead), &(fileHead.stcHead), sizeof(HeadInfo));
        return *this;
    }
}FileHead;

typedef struct _foot_secret_frame_data
{

    _foot_secret_frame_data()
        {
            time = 0.f;
            index = 10;
            quat[0] = 1.0f; quat[1] = quat[2] = quat[3] = 0.f;
            memset(accel, 0, sizeof(float)* 3);
            memset(gyro, 0, sizeof(float)* 3);
            memset(mag, 0, sizeof(float)* 3);
            memset(pos, 0, sizeof(float)* 3);
            memset(pressure, 0, sizeof(uint32_t)* PressureDataCount);
        }

    float time;
    float quat[4]; //wxyz  四元数
    float accel[3];//xyz   加速度
    float gyro[3]; //xyz   角速度
    float mag[3]; //xyz   磁力计
    float pos[3]; //xyz    位移
    uint32_t pressure[PressureDataCount];  //压力

    uint8_t index; //节点编号


}FSFrameData;

class uploadFile :public QObject
{
public:
    uploadFile();
    void upload();
    void GetGaitDir();
    void slotExportOriginalDataCSV(QString tmpFileName);
    void moveFiletoTempPath(QStringList file_list);
    void generateFileName(QString tmpFileName);
    void createZipFile(QString tempPath);

private slots:
    void uploadReply();
    void downloadReply();
    void downloadFinish();
    void contxt1();
    void contxt2();
    void addSuggestion();
    void abolishSuggestion();
public slots:
      void contxt3();
private:
    QString WorkFloder, Desktop_path, HspNum, device_num, SaveFloder,UploadZipName;
    QSystemTrayIcon *m_SystemTrayIcon;
    volatile bool stopped;
    QFile *file;
    QFile *myFile;
    uint8_t NodeSuitIDLeftFoot = 13;
    uint8_t NodeSuitIDRightFoot = 16;

    void showPdf(QString pdfName);
    void encodeAES();
    QString pdfName;
    QString token;
    void openFile();

    void uploadFiles(QString zipName);
    void downloadPdf(QString Api,QString name);
    const QString uploadFilesApi = "/scanning/uploadFile/uploadFootFile";
    const QString downloadGaitPdfApi = "/scanning/downloadFile/downloadGaitPdf/";
    const QString downloadBalancePdfApi = "/scanning/downloadFile/downloadBalancePdf/";
    const QString baseUrl = "http://localhost:8880";
//    const QString baseUrl = "http://121.36.217.102:8880";
    QNetworkReply *reply;
    QNetworkAccessManager *m_manager;

    int printContentChoose;//选择打印步态/平衡pdf选项
    LoadingDialog *loadingDialog;

//    void waitForServer();
signals:
//    void serverReady();
    void startWait();
    void stopWait();
    void endGait();     //新加

};

#endif // UPLOADFILE_H
